<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 17/4/12
 * Time: 17:43
 */
class Device_model extends MYNTModel {
  
    function __construct() {
        parent::__construct();
    }

//    public function getDevices($page, $snOrUserId) {
//        $param = " is_deleted = 0";
//        if (!empty($snOrUserId)) {
//            $userId  = $this->idcrypt->decode_user_id($snOrUserId);
//            if (!empty($userId)) {
//                $param .= " and (sn like '%".$snOrUserId."%' or user_id= ".$userId.")";
//            } else {
//                $param .= " and sn like '%".$snOrUserId."%'";
//            }
//        }
//        $start = ($page - 1) * $this->pageSize;
//        $query = $this->db->query('SELECT * from ' . $this->getTable(self::TABLE_DEVICE) . " where  ".$param." limit " . $start . ", " . $this->pageSize);
//        $countSql = "select count(*)  from " . $this->getTable(self::TABLE_DEVICE) . " where " .$param;
//        $queryCount = $this->db->query($countSql) -> row_array();
//        return array("total" => $queryCount["count(*)"], "data" => $query->result_array());
//
//    }


    public function get_devices($start, $length, $sn = '', $user_id ='', $order_by_createtime, $device_type) {

//        if (!empty($sn)) {
//            $this->db->like('sn', $sn);
//        }

        if (is_numeric($user_id)) {
            $userId  = $this->idcrypt->decode_user_id($user_id);
            if (!empty($userId)) {
                $this->db->where('user_id', $userId);
            }
        } else {
            $this->db->like('sn', $sn);
        }


//        if (!empty($user_id)) {
//            $userId  = $this->idcrypt->decode_user_id($user_id);
//            if (!empty($userId)) {
//                $this->db->where('user_id', $userId);
//            }
//        }

        if($device_type != -1) {
            $this->db->where('mynt_type', $device_type);
        }

        $this->db->where('is_deleted', 0);

        $orderby = array("create_time" => $order_by_createtime);

        return $this->getPageDataTables($start, $length, self::TABLE_DEVICE, $orderby);
    }

    public function get_device_count($device_type, $all, $betweenTime = array()) {



        $countSql = "select count(distinct sn) as dcount from mt_device";

        $where = " where 1= 1";
        if (!$all) {
            $where .= " and is_deleted = 0";
        }

        if ($device_type != -1) {
            $where.= " and mynt_type= {$device_type}";

        }

        if (!empty($betweenTime)) {
            $where.= " and create_time>= {$betweenTime['starttime']} and create_time < {$betweenTime['endtime']}";
        }

        $countSql.= $where;
        $queryCount = $this->db->query($countSql) -> row_array();
        return $queryCount['dcount'];
    }


    public function get_device_user_count($all, $betweenTime = array()) {

        $sql = "select count(distinct user_id) as ucount from mt_device";
        $where = " where 1=1";

        if (!$all) {
            $where.= " and is_deleted = 0";

        }

        if (!empty($betweenTime)) {
            $where.= " and create_time>= {$betweenTime['starttime']} and create_time < {$betweenTime['endtime']}";
        }

        $queryCount = $this->db->query($sql. $where) -> row_array();
        return $queryCount['ucount'];
    }


    public function unBind($deviceId) {
        $this->db ->set("is_deleted", 1)
            -> where("device_id", $deviceId)
            -> update(self::TABLE_DEVICE);
         return $this->db->affected_rows();
    }

    public function getDevice($deviceId) {
        $resData =  $this->db->select('*')
        ->from(self::TABLE_DEVICE)
         -> where("device_id", $deviceId)
        -> get() -> result_array();
        return $resData[0];
    }

    public function getMTControl($sn) {
        return $this->db->select('*')
            ->from("control_value")
            -> where("sn", $sn)
            -> get() -> result_array();
    }

    public function getAntilLost($sn) {
        return $this->db->select('*')
            ->from("usage_value")
            -> where("sn", $sn)
            -> get() -> result_array();
    }


    public function get_foreigns($lang) {
       return $this->db->select("distinct(user_id), lang")
            ->from(self::TABLE_DEVICE)
            ->where("lang", $lang)
            ->get()
            ->result_array();
    }
}